# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/12/1

import os

from submodules.common.lib.local_paths import project_path
from lib.json_ import trans_jsonFile_to_dict
from lib.log_ import logger


def get_obj_path(easy_path):
    list_tmp = easy_path.split(".")
    obj_json_file_path = os.path.join(project_path, "visual_obj", list_tmp[0], list_tmp[1], "base.json")
    obj_page_image_path = os.path.join(project_path, "visual_obj", list_tmp[0], list_tmp[1], "base.png")
    obj_ele_image_path = None
    if len(list_tmp) > 2:
        obj_ele_image_path = os.path.join(project_path, "visual_obj", list_tmp[0], list_tmp[1], list_tmp[2] + ".png")
    return obj_json_file_path, obj_page_image_path, obj_ele_image_path


def get_obj_attr(easy_path):
    '''

    :param easy_path:
    :return:
    ('验证码文本框', {'PATH': 'TOUTIAO.输入验证码页', '验证码文本框': {'id': '', 'xpath': '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/com.bytedance.android.gaia.activity.slideback.SlideFrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.ScrollView/android.widget.LinearLayout/android.widget.RelativeLayout[2]/android.widget.RelativeLayout/android.widget.LinearLayout/android.widget.EditText[1]', 'text': '', 'bounds': '[83,678][188,766]', 'class': '', 'package': ''}})
    '''
    easy_path_list = easy_path.split(".")
    try:
        client_type_name = easy_path_list[0]
        page_name = easy_path_list[1]
        ele_name = easy_path_list[2]
    except Exception as e:
        logger.error(easy_path)
        logger.error(repr(e))

    json_file_path = os.path.join(project_path, "visual_obj", client_type_name, page_name, "base.json")
    page_dict = trans_jsonFile_to_dict(json_file_path)
    return ele_name, page_dict


# def get_element_current_image(easy_path):
#     obj_attr_list = get_obj_attr(easy_path)
#     element_name = obj_attr_list[0]
#     page_dict = obj_attr_list[1]
#     bounds = page_dict["elements"][element_name].get("bounds")
#     if bounds == None or bounds == "":
#         logger.error("key error bounds!easy_path:%s" % easy_path)
#     else:
#         bounds_tuple = tuple(format_bounds_str_to_list(bounds))
#
#     current_image_path = get_current_shot(easy_path)
#     element_image_path = img_cut(current_image_path, bounds_tuple)
#     return element_image_path


if __name__ == "__main__":
    # test get_obj_path()
    # easy_path = "SGAME.签到页面.领取按钮"
    # logger.info(get_obj_path(easy_path))
    # easy_path = "SGAME.签到页面"
    # logger.info(get_obj_path(easy_path))

    # test get_obj_attr()
    # res = get_element_current_image("SGAME.登录页.开始游戏按钮")
    # logger.info(res)

    pass
